// 1.
type Coord = [number, number];
function getDist (arr: Coord[], [x2, y2]: Coord) {
    const distArr: number[] = [];
    for (const [x1, y1] of arr) {
        const dist = Math.sqrt( (x1-x2)**2 + (y1-y2)**2 ); // (x轴距离差的平方 + y轴距离差的平方) 开方
        distArr.push(dist);
    }
    return distArr;
}

const coordList: Coord[] = [
    [1, 2],
    [3, 4],
    [4, 5],
    [1.5, 1.5],
];

// console.log( getDist(coordList, [3, 2]) );

// 2.
interface DataItem {
    name: string;
    value: number;
}

interface Item {
    data: DataItem[];
}

// 普通人
// function getSum (arr: Item[]) {
//     let sum = 0;
//     for (const item of arr) {
//         for (const { value } of item.data) {
//             sum += value;
//         }
//     }
//     return sum;
// }

// 一般人(函数式)
// [1, 2, 3, 4]
function getNumSum (arr: number[]): number {
    let sum = 0;
    for (const num of arr) {
        sum += num;
    }
    return sum;
}

function getSum (arr: Item[]): number {
    const numArr: number[] = arr
        .map(item => item.data)
        .flat()
        .map(item => item.value);
    return getNumSum(numArr);
}

// 高质量
function getNumSum1 (arr: number[]): number {
    return arr.reduce((prev, curr) => prev + curr);
}

function getSum1 (arr: Item[]): number {
    const numArr: number[] = arr
        .map(item => item.data)
        .flat()
        .map(item => item.value);
    return getNumSum(numArr);
}

const list: Item[] = [
    {
        data: [
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 6457 },
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 75678 },
            { name: 'zhangsan', value: 1324 },
        ],
    },
    {
        data: [
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 4577 },
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 64572 },
            { name: 'zhangsan', value: 1324 },
        ],
    },
    {
        data: [
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 1249875 },
            { name: 'zhangsan', value: 1324 },
            { name: 'zhangsan', value: 985904385 },
            { name: 'zhangsan', value: 1324 },
        ],
    },
];

// console.log( getNumSum1([1, 2, 3, 4]) ); // 10
const numList: number[] = new Array(1000).fill(0).map(() => Math.floor(Math.random()));

// console.time('一般人');
// for (let i = 0; i < 10000000; i++) {
//     getNumSum(numList);
// }
// console.timeEnd('一般人');

// console.time('高质量');
// for (let i = 0; i < 10000000; i++) {
//     getNumSum1(numList);
// }
// console.timeEnd('高质量');

// 3.
// 高质量
function unique (arr: number[]): number[] {
    const uniqueArr: number[] = [];
    for (const num of arr) {
        // 没找到的话把num  push到结果数组中
        if (!uniqueArr.some(n => n === num)) {
            uniqueArr.push(num);
        }
    }
    return uniqueArr;
}

// 一般人
function unique1 (arr: number[]): number[] {
    // O(n^2)
    return arr.filter((item, i) => {
        return arr.indexOf(item) === i;
    });
}

// console.time('一般人');
// for (let i = 0; i < 1000000; i++) {
//     unique(numList);
// }
// console.timeEnd('一般人');

// console.time('高质量');
// for (let i = 0; i < 1000000; i++) {
//     unique1(numList);
// }
// console.timeEnd('高质量');

// 4.
// 暴力求解 O(N^2)
function twoNum (arr: number[], target: number): [number, number] {
    const len = arr.length;
    for (let i = 0; i < len; i++) {
        for (let j = i + 1; j < len; j++) {
            if (arr[i] + arr[j] === target) {
                return [i, j];
            }
        }
    }
}

// 高质量 1 （哈希表方式）O(N)
function twoNum1 (arr: number[], target: number): [number, number] {
    // 映射
    const map = {};
    for (let i = 0; i < arr.length; i++) {
        const current = arr[i];
        const tmp = target - current; // 要找的值
        if (map[tmp] !== undefined) {
            return [ map[tmp], i ];
        }
        map[current] = i;
    }
}

console.time('一般人');
for (let i = 0; i < 10000; i++) {
    twoNum(numList, 100);
}
console.timeEnd('一般人');

console.time('高质量');
for (let i = 0; i < 10000; i++) {
    twoNum1(numList, 100);
}
console.timeEnd('高质量');

